package com.example.usercenter.controller;

import com.example.usercenter.common.ErrorCode;
import com.example.usercenter.common.Result;
import com.example.usercenter.common.ResultUtils;
import com.example.usercenter.exception.ServiceException;
import com.example.usercenter.model.domain.User;
import com.example.usercenter.model.request.*;
import com.example.usercenter.model.vo.TeamVO;
import com.example.usercenter.service.TeamService;
import com.example.usercenter.service.UserService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpSession;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/team")
public class TeamController {
    @Resource
    private TeamService teamService;
    @Resource
    private UserService userService;

    @PostMapping("/create")
    public Result<Long> createTeam(@RequestBody TeamCreateRequest TeamCreateRequest, HttpSession session) {
        // 校验参数
        if (TeamCreateRequest == null) {
            throw new ServiceException(ErrorCode.PARAMS_NULL);
        }
        // 获取登录用户
        User loginUser = userService.getLoginUser(session);
        // 创建新队伍
        long teamId = teamService.createTeam(TeamCreateRequest, loginUser);
        return ResultUtils.success(teamId);
    }

    @PostMapping("/add")
    public Result<Boolean> addTeam(@RequestBody TeamAddRequest teamAddRequest, HttpSession session) {
        // 校验参数
        if (teamAddRequest == null) {
            throw new ServiceException(ErrorCode.PARAMS_NULL);
        }
        // 获取登录用户
        User loginUser = userService.getLoginUser(session);
        // 创建新队伍
        boolean addTeamRes = teamService.addTeam(teamAddRequest, loginUser);
        return ResultUtils.success(addTeamRes);
    }

    @PostMapping("/quit")
    public Result<Boolean> quitTeam(@RequestBody TeamQuitRequest teamQuitRequest, HttpSession session) {
        // 校验参数
        if (teamQuitRequest == null) {
            throw new ServiceException(ErrorCode.PARAMS_NULL);
        }
        // 获取登录用户
        User loginUser = userService.getLoginUser(session);
        // 创建新队伍
        boolean quitTeamRes = teamService.quitTeam(teamQuitRequest, loginUser);
        return ResultUtils.success(quitTeamRes);
    }

    @GetMapping("/list")
    public Result<List<TeamVO>> showTeamList() {
        // 返回队伍列表信息
        return ResultUtils.success(teamService.showTeamList());
    }

    @PostMapping("/dissolution")
    public Result<Boolean> dissolutionTeam(@RequestBody TeamDissolutionRequest teamDissolutionRequest, HttpSession session) {
        // 1. 参数校验
        if (teamDissolutionRequest == null) {
            throw new ServiceException(ErrorCode.PARAMS_NULL);
        }
        // 2. 获取当前登录用户
        User loginUser = userService.getLoginUser(session);
        // 3. 执行解散队伍业务
        return ResultUtils.success(teamService.dissolutionTeam(teamDissolutionRequest, loginUser));
    }

    @PostMapping("/update")
    public Result<Boolean> updateTeam(@RequestBody TeamUpdateRequest teamUpdateRequest, HttpSession session) {
        // 1. 参数校验
        if (teamUpdateRequest == null) {
            throw new ServiceException(ErrorCode.PARAMS_NULL);
        }
        // 2. 获取当前登录用户
        User loginUser = userService.getLoginUser(session);
        // 3. 执行修改队伍业务
        return ResultUtils.success(teamService.updateTeam(teamUpdateRequest, loginUser));
    }

    @GetMapping("/my/create")
    public Result<List<TeamVO>> getCreateTeamList(HttpSession session) {
        // 1. 获取当前登录用户
        User loginUser = userService.getLoginUser(session);
        // 2. 执行我的创建队伍业务
        return ResultUtils.success(teamService.showCreateTeamList(loginUser));
    }

    @GetMapping("/my/join")
    public Result<List<TeamVO>> getJoinTeamList(HttpSession session) {
        // 1. 获取当前登录用户
        User loginUser = userService.getLoginUser(session);
        // 2. 执行我的加入队伍业务
        return ResultUtils.success(teamService.showJoinTeamList(loginUser));
    }
}
